﻿#if !NETFX_CORE
using Cofe.Core.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cofe.Core.Implements
{
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFE)]
    public class MEFServiceImporter : IServiceImporter
    {
        public MEFServiceImporter(string libraryPath)
        {
            _libraryPath = libraryPath;
        }

        public MEFServiceImporter() : this(null)
        {            
        }

        public IEnumerable<ICofeService> ImportServices()
        {
            if (_libraryPath == null)
                _libraryPath = ".";

            using (var catalog = new DirectoryCatalog(_libraryPath))
            {
                using (var container = new CompositionContainer(catalog))
                {
                    container.ComposeParts(this);
                }
            }
            return _services;
        }

        private string _libraryPath = null;

        [ImportMany(typeof(ICofeService))]
        private IEnumerable<ICofeService> _services { get; set; }
    }
}
#endif